package com.chap13;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;

/**
 * <p>
 * <code>Fibonacci</code>
 * </p>
 *
 * @author junbao3
 * TODO
 * @date 2023-04-12 13:58
 * Copyright (C) 2021 IFlyTek. All rights reserved.
 */
public class Fibonacci extends RecursiveTask<Integer> {
    int n;

    public Fibonacci(int n) {
        this.n = n;
    }


    @Override
    protected Integer compute() {
        if (n<=1){
            return n;
        }
        Fibonacci f1 = new Fibonacci(n-1);
        f1.fork();
        Fibonacci f2 = new Fibonacci(n-2);
        //f2.compute()实际就是f2.join()
        return f2.compute()+f1.join();
    }

    public static void main(String[] args) {
        ForkJoinPool forkJoinPool = new ForkJoinPool();
        Fibonacci fibonacci = new Fibonacci(100000);
        Integer result = forkJoinPool.invoke(fibonacci);
        System.out.println(result);
    }
}
